<?php defined('SYSPATH') or die('No direct script access.');
require ('application/classes/dao/base.php');
require ('application/classes/dao/ValidateCode.php');
require ('application/classes/dao/Captcha.php');
class Controller_reg extends Controller {


	public function action_regUser(){
		echo View::factory('web/reg');
	}

	public function action_checkUserName(){
		$parm = get($this->request);
		$result = null;
		try{
			if(!isset($parm['username']) ||($parm['username'])==""){
				$result['msg']='用户名不能为空';
				return;
			}
			$entity = rpcCall('user.getByUserName',$parm);
			$result['msg'] = 'success';
			if(count($entity) == 0)
				$result['code'] = 2;
			else
				$result['code'] = 1;
		}catch (Exception $e){
			$result['msg']='检查用户名失败请刷新再试';
			$result['code'] = -1;
			$result['error'] =$e->getMessage();
		}finally{
			echo json_encode($result);
		}

	}

	public function action_getCode(){
		$captcha = new Captcha(163,64,6);
//setcookie('session_name1',$captcha->getCaptcha());

		$captcha->showImg();
		setsession('code',$captcha->getCaptcha());
		//kohana框架限制 如果die去掉会图片不显示
		die;
//		Image::
	}
	public function action_sendRegPhone(){
		$parm = get($this->request);
		$result = null;
		try{
			if(!isset($parm['phone']) ||($parm['phone'])==""){
				echo "手机不能为空";
				exit;
			}
			$checkcode =  rand(100000, 999999);
			$parm['use'] = "未使用";
			$parm['model'] = '用户注册';
			$parm['ValidateCode']=$checkcode;
			$parm['action'] = 'send';
			$getresult = rpcCall('checkcode.sendCode',$parm);

			if($getresult['flag']){
				$content = '欢迎注册开源商城,您的验证码:';
//			$result['msg'] = sendSMS($parm['phone'],$content.$checkcode);
				$result['msg'] = true;
				return ;
			}else{
				$result['data'] = $getresult['data'];
				$result['msg'] = false;
			}


		}catch (Exception $e){
			$result['msg']='检查用户名失败请刷新再试';
			$result['code'] = -1;
			$result['error'] =$e->getMessage();
		}finally{
			echo json_encode($result);
		}

	}

	public function action_reg(){

		$parm = post($this->request);
		$result = null;
		try {
//			print_r($parm);
			$msg = "";
			if (!isset($parm['username']) || ($parm['username']) == "") {
				$msg="用户名不能为空";
				return;
			}
			if (!isset($parm['pageCode']) || ($parm['pageCode']) == "") {
				$msg="页面验证码不能为空";
				return;
			}
			if (!isset($parm['phone']) || ($parm['phone']) == "") {
				$msg="手机号不能为空";
				return;
			}
			if (!isset($parm['password']) || ($parm['password']) == "") {
				$msg="密码不能为空";
				return;
			}
			if (!isset($parm['code']) || ($parm['code']) == "") {
				$msg="验证码不能为空";
				return;
			}
			Kohana::$log->add(Log::INFO,$parm['phone'].'用户注册');
			if(strtolower(getSession('code')) !=strtolower($parm['pageCode'])){
				$msg="页面验证码不正确";
				return;
			}

			$result = rpcCall('user.regUser',$parm);
			$msg =  $result;

		}catch (Exception $e){
			$msg = "注册失败";
			Kohana::$log->add(Log::ERROR,$parm['phone'].'发生异常:'.$e->getMessage());
		}finally{
			if($msg ==""){
				Kohana::$log->add(Log::INFO,$parm['phone'].'用户注册成功');
				echo "<script>window.location.href='".Url::base()."'</script>";
			}else{
				echo View::factory('web/reg')->set("parm",$parm)->set('msg',$msg);
			}
		}
	}

}
